<?php


// THIS FILE NOT YET USED


// gets the game from the database and saves it into the iShudan class
function load_game($game_id)
{
	global $mysql;
	global $iShudan;

	// clear out the previous data
	$_SESSION['gamedata'] = null;
	unset($_SESSION['gamedata']);

	$query = "
		SELECT *
		FROM ".T_GAME."
		WHERE g_id = '{$game_id}'
	";
	$gamedata = $mysql->fetch_assoc($query, __LINE__, __FILE__);

	$_SESSION['game_id'] = $game_id;
	$_SESSION['gamedata']['status']      = $gamedata['g_status'];
	$_SESSION['gamedata']['board_size']  = $gamedata['g_board_size'];
	$_SESSION['gamedata']['handicap']    = $gamedata['g_handicap'];
	$_SESSION['gamedata']['move_string'] = $gamedata['g_moves'];
	$_SESSION['gamedata']['num_moves']   = $gamedata['g_num_moves'];
	$_SESSION['gamedata']['moves']       = explode(',', $gamedata['g_moves']);
	$_SESSION['gamedata']['counter']     = count($_SESSION['gamedata']['moves']);

	$query = "
		SELECT *
		FROM ".T_PLAYER."
		WHERE p_id = '{$gamedata['g_black_player_id']}'
	";
	$gamedata['p_black'] = $mysql->fetch_assoc($query, __LINE__, __FILE__);

	$_SESSION['gamedata']['black']['id']       = $gamedata['p_black']['p_id'];
	$_SESSION['gamedata']['black']['username'] = $gamedata['p_black']['p_username'];
	$_SESSION['gamedata']['black']['email']    = $gamedata['p_black']['p_email'];

	$query = "
		SELECT *
		FROM ".T_PLAYER."
		WHERE p_id = '{$gamedata['g_white_player_id']}'
	";
	$gamedata['p_white'] = $mysql->fetch_assoc($query, __LINE__, __FILE__);

	$_SESSION['gamedata']['white']['id']       = $gamedata['p_white']['p_id'];
	$_SESSION['gamedata']['white']['username'] = $gamedata['p_white']['p_username'];
	$_SESSION['gamedata']['white']['email']    = $gamedata['p_white']['p_email'];

	if (('Black' == $gamedata['g_status']) || ('White' == $gamedata['g_status']))
	{
		$_SESSION['gamedata'][strtolower($gamedata['g_status'])]['move'] = true;
	}

	if ($_SESSION['player_id'] == $gamedata['g_black_player_id'])
	{
		$_SESSION['gamedata']['player'] = $_SESSION['gamedata']['black'];
		$_SESSION['gamedata']['player']['color'] = 'black';

		$_SESSION['gamedata']['opponent'] = $_SESSION['gamedata']['white'];
		$_SESSION['gamedata']['opponent']['color'] = 'white';
	}
	else
	{
		$_SESSION['gamedata']['player'] = $_SESSION['gamedata']['white'];
		$_SESSION['gamedata']['player']['color'] = 'white';

		$_SESSION['gamedata']['opponent'] = $_SESSION['gamedata']['black'];
		$_SESSION['gamedata']['opponent']['color'] = 'black';
	}

	// make sure ['player']['move'] has data
	if ( ! isset($_SESSION['gamedata']['player']['move']))
	{
		$_SESSION['gamedata']['player']['move'] = false;
	}

	return true;
}


// gets the game data from the iShudan class and saves it to the database
function save_game($game_id, $move, $repeats, $removed = '')
{
	global $mysql;
	global $iShudan;

	// get the current values
	$status      = $_SESSION['gamedata']['status'];
	$num_moves   = $_SESSION['gamedata']['num_moves'];
	$move_string = $_SESSION['gamedata']['move_string'];

	$PASSED_SQL = '';

	if (substr($move, 1, 2) == 'zz') // resigned
	{
		if (substr($move, 0, 1) == 'b')
		{
			$status = 'Black Resigned';
		}
		else
		{
			$status = 'White Resigned';
		}

		$move_string .= ',' . $move;
	}
	else if (substr($move, 1, 2) == 'xx') // passed
	{
		if (intval($repeats) == 2)
		{
			$status = 'Game Over';
		}

		$move_string .= ',' . $move;
		$num_moves++;
		$PASSED_SQL = ' , g_passed = 1 ';
	}
	else // normal move
	{
		$move_string .= ',' . $move . $removed;
		$num_moves++;
		$status = ('Black' == $status) ? 'White' : 'Black';
	}

	$query = "
		UPDATE ".T_GAME."
		SET g_moves = '{$move_string}'
			, g_num_moves = '{$num_moves}'
			, g_status = '{$status}'
			{$PASSED_SQL}
		WHERE g_id = '{$_SESSION['game_id']}'
	";
	$mysql->query($query, __LINE__, __FILE__);

	return true;
}

?>